package com.nttuan.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.nttuan.util.Support;
import com.nttuan.model.DataBaseHelper;
import com.nttuan.model.dao.service.LocationService;
import com.nttuan.model.entity.Location;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by NT Tuan on 10/21/2014.
 */
public class LocationDao implements LocationService{
    private DataBaseHelper db = null;
    private static LocationDao locationDao;

    public static LocationDao getInstance(Context context){
        if(locationDao == null)
            locationDao = new LocationDao(context);
        return locationDao;
    }
    public LocationDao(Context context){
        try{
            db = new DataBaseHelper(context);
        }catch (Exception e){}
    }

    @Override
    public List<String> listAddress() {
        db.openDataBase();
        List<String> result = new ArrayList<String>();
        Cursor cursor = db.select(true,Location.TABLE_NAME,null,null,null,null);
        if(cursor != null)
            while (cursor.moveToNext())
                result.add(cursor.getString(cursor.getColumnIndex(Location.ADDRESS)));
        db.close();
        return  result;
    }

    @Override
    public Location crateOrUpdate(Location location) {
        int newId = isExit(location.getAddress());
        if(newId == 0) {
            create(location);
            newId = newId();
        }
        return location(newId);
    }

    @Override
    public Location crateOrUpdate(String address) {
        Location location = new Location(0, address);
        int newId = isExit(address);
        if(newId == 0) {
            create(location);
            newId = newId();
        }
        return location(newId);
    }

    private int newId() {
        db.openDataBase();
        String[] column = {"max(" + Location.LOCATION_ID + ") as newId"};
        Cursor cursor = db.select(true, Location.TABLE_NAME, column, null, null, null);
        if (cursor != null)
            if (cursor.moveToNext()) {
                db.close();
                return cursor.getInt(cursor.getColumnIndex("newId"));
            }
        return 1;
    }
    @Override
    public int isExit(String address) {
        db.openDataBase();
        String selection = Location.ADDRESS +"=?";
        String[] where = {address};
        Cursor cursor = db.select(true,Location.TABLE_NAME,null,selection,where,null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return cursor.getInt(cursor.getColumnIndex(Location.LOCATION_ID));
            }
        return  0;
    }

    @Override
    public List<Location> locations(boolean isActive) {
        db.openDataBase();
        List<Location> result = new ArrayList<Location>();
        String selection = Location.IS_ACTIVE +"=?";
        String[] where = {isActive ? "1":"0"};
        Cursor cursor = db.select(true,Location.TABLE_NAME,null,selection,where,null);
        if(cursor != null)
            while (cursor.moveToNext())
                result.add(getItem(cursor));
        db.close();
        return  result;
    }

    @Override
    public Location location(int locationId, boolean isActive) {
        db.openDataBase();
        String selection = Location.LOCATION_ID +"=? and " + Location.IS_ACTIVE +"=? ";
        String[] where = {locationId+"",isActive ? "1":"0"};
        Cursor cursor = db.select(true,Location.TABLE_NAME,null,selection,where,null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return getItem(cursor);
            }
        return  null;
    }

    @Override
    public Location location(int locationId) {
        db.openDataBase();
        String selection = Location.LOCATION_ID +"=?";
        String[] where = {locationId+""};
        Cursor cursor = db.select(true,Location.TABLE_NAME,null,selection,where,null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return getItem(cursor);
            }
        return  null;
    }

    @Override
    public Location location(String address) {
        db.openDataBase();
        String selection = Location.ADDRESS +"=?";
        String[] where = {address+""};
        Cursor cursor = db.select(true,Location.TABLE_NAME,null,selection,where,null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return getItem(cursor);
            }
        return null;
    }

    @Override
    public boolean create(Location location) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        //   values.put(Category.CATEGORY_ID,category.getCategoryId());
    //    values.put(Location.LOCATION_ID,location.getLocationId());
        values.put(Location.ADDRESS,location.getAddress());
        values.put(location.LOG_LAG,location.getLog_lag());
        values.put(Location.NOTE,location.getNote());
        values.put(location.IS_ACTIVE, location.isActive());
        boolean result = db.insert(Location.TABLE_NAME,values);
        db.close();
        return result;
    }

    @Override
    public boolean update(Location location) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        values.put(Location.ADDRESS,location.getAddress());
        values.put(Location.LOG_LAG,location.getLog_lag());
        values.put(Location.NOTE,location.getNote());
        values.put(Location.IS_ACTIVE,location.isActive());
        String whereClause = Location.LOCATION_ID +" =?";
        String[] whereArgs = {location.getLocationId()+""};
        boolean result = db.update(Location.TABLE_NAME,values,whereClause,whereArgs);
        db.close();
        return result;
    }

    @Override
    public boolean remove(int locationId) {
        db.openDataBase();
        String whereClause = Location.LOCATION_ID + "=?";
        String[] whereArgs = {locationId+""};
        boolean result = db.delete(Location.TABLE_NAME,whereClause,whereArgs);
        db.close();
        return result;
    }

    @Override
    public boolean active(int locationId, boolean isActive) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        values.put(Location.IS_ACTIVE,isActive);
        String whereClause = Location.LOCATION_ID +" =?";
        String[] whereArgs = {locationId+""};
        boolean result = db.update(Location.TABLE_NAME,values,whereClause,whereArgs);
        db.close();
        return result;
    }

    @Override
    public boolean isExit(Location location) {
        return location(location.getAddress()) != null;
    }
    private Location getItem(Cursor cursor){
        Location location = new Location();
        location.setLocationId(cursor.getInt(cursor.getColumnIndex(Location.LOCATION_ID)));
        location.setAddress(cursor.getString(cursor.getColumnIndex(Location.ADDRESS)));
        location.setLog_lag(cursor.getString(cursor.getColumnIndex(Location.LOG_LAG)));
        location.setNote(cursor.getString(cursor.getColumnIndex(Location.NOTE)));
        location.setActive(Support.toActive(cursor.getString(cursor.getColumnIndex(Location.IS_ACTIVE))));
        return location;
    }
}
